package com.t.bricks.bussiness.service.bridge.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.dao.bridge.BridgeDao;
import com.t.bricks.bussiness.service.bridge.LoginService;
import com.t.bricks.model.MsgEmity;

/**
 * 登录接口桥接实现
 * @author huangxinbian
 */
@ConditionalOnMissingClass("com.t.bricks.bussiness.server.login.impl.LoginServerImpl")
@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private BridgeDao bridgeDao;
	
	/**
	 * 桥接到登录服务的服务名,默认TBaseSystemServer
	 */
	@Value("${CloudServerBaseServer:TBaseSystemServer}")
	private String loginServiceName;

	/**
	 * 登录,如果用户和密码正确则返回同行令牌
	 * @param sNameOrNo 名称或工号
	 * @param sPass 密码
	 * @param sOwner 用户来源表
	 * @param iDevice 设备类型,1:PC,2:手机,3:平板,4.....
	 * @return
	 */
	@Override
	public MsgEmity in(String sNameOrNo, String sPass, String sOwner, Integer iDevice) {
		Map<String, Object> map = new HashMap<>(4);
		map.put("sNameOrNo", sNameOrNo);
		map.put("sPass", sPass);
		map.put("sOwner", sOwner);
		map.put("iDevice", iDevice);
		
		MsgEmity me = bridgeDao.post(loginServiceName, "/login/in", map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}

	/**
	 * 用户登出
	 * @param sCookie
	 * @return
	 */
	@Override
	public MsgEmity out(String sCookie) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sCookie", sCookie);
		
		MsgEmity me = bridgeDao.post(loginServiceName, "/login/out", map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}

	/**
	 * 判断sCookie是否已经登录
	 * @param sCookie
	 * @return
	 */
	@Override
	public MsgEmity check(String sCookie) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sCookie", sCookie);
		
		MsgEmity me = bridgeDao.post(loginServiceName, "/login/check", map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}

	/**
	 * 登录心跳操作,sCookie存在则更新并返回true,没有则返回false
	 * @param sCookie
	 * @return
	 */
	@Override
	public MsgEmity heartbeat(String sCookie) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sCookie", sCookie);
		
		MsgEmity me = bridgeDao.post(loginServiceName, "/login/heartbeat", map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
	/**
	 * 取登录用户信息,注意:限制为内部系统访问
	 * @param key
	 * @param sCookie
	 * @return
	 */
	@Override
	public MsgEmity getLogin(String key, String sCookie) {
		Map<String, Object> map = new HashMap<>(2);
		map.put("key", key);
		map.put("sCookie", sCookie);
		
		MsgEmity me = bridgeDao.post(loginServiceName, "/login/info", map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
	/**
	 * 取当前登录用户简洁信息
	 * @param request
	 * @return
	 */
	@Override
	public MsgEmity getCurrentLogin() {
		MsgEmity me = bridgeDao.post(loginServiceName, "/login/info/current", null, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}

	/**
	 * 根据用户和密码取对应的用户编号
	 * @param sNameOrNo
	 * @param sPass
	 * @param sOwner 用户来源表
	 * @return
	 */
	@Override
	public MsgEmity getUserId(String sNameOrNo, String sPass, String sOwner) {
		Map<String, Object> map = new HashMap<>(3);
		map.put("sNameOrNo", sNameOrNo);
		map.put("sPass", sPass);
		map.put("sOwner", sOwner);
		
		MsgEmity me = bridgeDao.post(loginServiceName, "/login/getid", map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
}
